package com.cd.javacxy.module.web.controller;

import com.cd.javacxy.common.model.entity.CxyUserFriend;
import com.cd.javacxy.common.model.entity.SessionUser;
import com.cd.javacxy.common.model.enums.ResponseCode;
import com.cd.javacxy.common.model.query.UserFriendQuery;
import com.cd.javacxy.common.model.vo.AjaxResponse;
import com.cd.javacxy.common.utils.Constants;
import com.cd.javacxy.common.utils.PageResult;
import com.cd.javacxy.exception.BussinessException;
import com.cd.javacxy.module.base.BaseController;
import com.cd.javacxy.module.web.service.CxyUserFriendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * CxyUserFriend的路由接口服务
 *
 * @author yanghy
 */
@RestController
@RequestMapping("/friend")
@Slf4j
public class CxyUserFriendController extends BaseController {

    @Autowired
    private CxyUserFriendService cxyUserFriendService;

    @ResponseBody
    @RequestMapping("/user/list")
    public AjaxResponse<Object> loadUserFriend(HttpSession session, int pageNum) {
        AjaxResponse<Object> ajaxResponse = AjaxResponse.builder().build();
        SessionUser sessionUser = (SessionUser) session.getAttribute(Constants.SESSION_USER_KEY);
        if (sessionUser == null) {
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            ajaxResponse.setErrorMsg("请先登录");
            return ajaxResponse;
        }
        int userId = this.getUserId(session);
        UserFriendQuery userFriendQuery = new UserFriendQuery();
        userFriendQuery.setUserId(userId);
        userFriendQuery.setPageNum(pageNum);
        PageResult<CxyUserFriend> pageResult = this.cxyUserFriendService.findFriendList(userFriendQuery);
        ajaxResponse.setData(pageResult);
        return ajaxResponse;
    }

    @ResponseBody
    @GetMapping("/fans")
    public AjaxResponse<Object> loadUserFans(HttpSession session, UserFriendQuery userFriendQuery) {
        AjaxResponse<Object> ajaxResponse = AjaxResponse.builder().build();
        try {
            PageResult<CxyUserFriend> pageResult = this.cxyUserFriendService.findFansList(userFriendQuery);
            ajaxResponse.setData(pageResult);
        } catch (Exception e) {
            log.error("加载用户粉丝异常", e);
            ajaxResponse.setErrorMsg("加载粉丝出错");
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
        }
        return ajaxResponse;
    }

    @ResponseBody
    @GetMapping("/focus")
    public AjaxResponse<Object> loadUserFriend(HttpSession session, UserFriendQuery userFriendQuery) {
        AjaxResponse<Object> ajaxResponse = AjaxResponse.builder().build();
        try {
            PageResult<CxyUserFriend> pageResult = this.cxyUserFriendService.findFriendList(userFriendQuery);
            ajaxResponse.setData(pageResult);
        } catch (Exception e) {
            log.error("加载关注用户异常", e);
            ajaxResponse.setErrorMsg("加载关注用户出错");
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
        }
        return ajaxResponse;
    }

    @ResponseBody
    @PostMapping("/focus")
    public AjaxResponse<Object> focus(HttpSession session, CxyUserFriend userFriend) {
        AjaxResponse<Object> ajaxResponse = AjaxResponse.builder().build();
        try {
            this.setUserBaseInfo(CxyUserFriend.class, userFriend, session);
            this.cxyUserFriendService.addFocus(userFriend);
        } catch (BussinessException e) {
            log.error("关注异常", e);
            ajaxResponse.setErrorMsg(e.getLocalizedMessage());
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
        } catch (Exception e) {
            log.error("加载用户粉丝异常", e);
            ajaxResponse.setErrorMsg("关注出错,请重试");
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
        }
        return ajaxResponse;
    }

    @ResponseBody
    @DeleteMapping("/cancel/focus")
    public AjaxResponse<Object> cancelFocus(HttpSession session, CxyUserFriend userFriend) {
        AjaxResponse<Object> ajaxResponse = AjaxResponse.builder().build();
        try {
            this.setUserBaseInfo(CxyUserFriend.class, userFriend, session);
            this.cxyUserFriendService.cancelFocus(userFriend);
        } catch (BussinessException e) {
            log.error("关注异常", e);
            ajaxResponse.setErrorMsg(e.getLocalizedMessage());
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
        } catch (Exception e) {
            log.error("加载用户粉丝异常", e);
            ajaxResponse.setErrorMsg("关注出错,请重试");
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
        }
        return ajaxResponse;
    }
}
